package com.base.voicerecorod;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by aman on 16/4/18.
 */
public class RecorderAdapter extends ArrayAdapter<MainActivity.Recorder> {

    private List<MainActivity.Recorder> mDatas;
    private Context mContext;

    private int mMinItemWidth;
    private int mMaxItemWidth;

    private LayoutInflater mInflater;


    public RecorderAdapter(Context context, List<MainActivity.Recorder> datas) {
        super(context, -1, datas);
        mContext = context;
        mDatas = datas;

        mInflater = LayoutInflater.from(context);

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        DisplayMetrics outMetrcs = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrcs);

        mMaxItemWidth = (int) (outMetrcs.widthPixels*0.7f);
        mMinItemWidth = (int) (outMetrcs.widthPixels*0.15f);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_recorder, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.seconds = (TextView) convertView.findViewById(R.id.id_text);
            viewHolder.length = convertView.findViewById(R.id.id_recorder_length);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.seconds.setText(Math.round(getItem(position).getTime()) + "\"");
        ViewGroup.LayoutParams lp = viewHolder.length.getLayoutParams();
        lp.width = (int) (mMinItemWidth + (mMaxItemWidth/60f * getItem(position).getTime()));

        return convertView;
    }

    private class  ViewHolder
    {
        TextView seconds;
        View length;
    }
}
